package com.xiaode.controller.admin.login;

import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.Ret;
import com.xiaode.common.model.Admin;
import com.xiaode.interceptor.AdminAuthInterceptor;
import com.xiaode.util.SnowflakeIdWorker;
import com.xiaode.util.apidoc.annotation.Api;
import com.xiaode.util.apidoc.annotation.ApiOper;
import com.xiaode.util.apidoc.annotation.ApiPara;
import com.xiaode.util.welink.WeLinkUtil;

import javax.validation.constraints.Size;

/**
 * 登录服务
 */
@Path("/aLogin")
@Api("管理员登录相关服务")
@EnableCORS
public class LoginController extends Controller {

	@Inject
	private LoginService service;

	@ApiOper("welink重定向")
	@Clear(AdminAuthInterceptor.class)
	public void weLinkRedirect() {
		String redirect_uri = "https://login.welink.huaweicloud.com/sso/oauth2/authorize?client_id=" 
			+ WeLinkUtil.ClientId 
			+ "&response_type=code&redirect_uri=https://xd-admin.jidaosec.cn&scope=backendlogin&state=" 
			+ SnowflakeIdWorker.generateId();
		redirect(redirect_uri);
	}

	@ApiOper("用户登陆")
	@Clear(AdminAuthInterceptor.class)
	public void login(@ApiPara(value = "account", notes = "账号") String account,
			@ApiPara(value = "password", notes = "密码") String password,
			@ApiPara(value = "code", notes = "基座编码") String code,
			@ApiPara(value = "platform", notes = "平台(1app，2welink，3web，4微信)") Integer platform) throws Exception {
		if ((!validateCaptcha("captcha")) && (platform == 1 || platform == 3)) {
			renderJson(Ret.fail().set("msg", "验证码不正确")); return ; 
		}
		Ret ret = service.login(account, password, code, platform);
		renderJson(ret);
	}

	@ApiOper("绑定WElink")
	@Clear(AdminAuthInterceptor.class)
	public void binDingWeLink(@Size @ApiPara(value = "account", notes = "账号") String account,
			@Size @ApiPara(value = "password", notes = "密码") String password,
			@Size @ApiPara(value = "banDingCode", notes = "绑定码") String banDingCode) throws Exception {
		Ret ret = service.binDing(account, password, banDingCode);
		renderJson(ret);
	}

	/**
	 * 验证码接口 如果需要添加验证码功能，在 html 中通过 img 标签的 src 属性指向该 action：
	 * <img src="/admin/login/captcha" onclick="updateCaptcha(this);">
	 * 
	 * 后端在 Controller 中判断验证码是否正确： if ( ! validateCaptcha("captcha") ) {
	 * renderJson(Ret.fail("msg", "验证码不正确")); return ; } 以上代码中的 "captcha" 与页面 form
	 * 表单中输入验证码 input 的 name 属性值需要相同： <input type="text" name="captcha"> 该 name
	 * 属性名可以任意取名，不必要求是 captcha
	 */
	@ApiOper("获取验证码")
	@Clear({ AdminAuthInterceptor.class })
	public void getVCode(@ApiPara(value = "account", notes = "账号") String account) {
		renderCaptcha();
	}

	/**
	 * 退出
	 */
	@ApiOper("退出")
	public void outLogin() throws Exception {
		Admin admin = getAttr("admin");
		Ret ret = service.outLogin(admin);
		renderJson(ret);
	}

	/**
	 * 个人信息
	 */
	@ApiOper("获取个人信息")
	public void getMeInfo() throws Exception {
		Admin admin = getAttr("admin");
		Ret ret = service.getMeInfo(admin);
		renderJson(ret);
	}

}